// import fs from 'fs'
const fs = {}
// import rw from 'rw'
const rw = {}
import ShpConverterText from '../shpconverter-text'
import internal from '../common.js'

function fileSize(path) {
  const ss = statSync(path)
  return ss && ss.size || 0
}
// @encoding (optional) e.g. 'utf8'
function readFile(fname, encoding, cache) {
  let content = null
  if (cache && (fname in cache)) {
    content = cache[fname]
    delete cache[fname]
  } else if (fname == '/dev/stdin') {
    content = rw.readFileSync(fname)
  } else {
    internal.getStateVar('input_files').push(fname)
    content = fs.readFileSync(fname)
  }
  if (encoding && Buffer.isBuffer(content)) {
    content = ShpConverterText.trimBOM(ShpConverterText.decodeString(content, encoding))
  }
  return content
}
function statSync(fpath) {
  let obj = null
  try {
    obj = fs.statSync(fpath)
  } catch(e) {}
  return obj
}
export default {
  fileSize,
  readFile,
}